package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.BaseStepDao;
import com.linktel.productionLine.entity.dto.baseStep.BaseStepDTO;
import com.linktel.productionLine.entity.query.baseStep.ListBaseStepByMonthlyPlanningQuery;
import com.linktel.productionLine.exception.BusinessException;
import com.linktel.productionLine.util.MethodUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;


/**
 * 对工序base_step的操作
 *
 * @Author HuangJiaHui
 * @Date 2023/8/25 10:26
 */
@Service
@AllArgsConstructor
public class BaseStepService {

    private BaseStepDao baseStepDao;

    /**
     * 查询所有工序
     *
     * @return 工序列表
     */
    public List<BaseStepDTO> listBaseStep() {
        return baseStepDao.listBaseStep();
    }

    /**
     * 根据关键工序id查询关键工序名称
     *
     * @return 关键工序名称
     */
    public String getBaseStepNameByCode(String keyStepCode) {
        if (keyStepCode == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return baseStepDao.getBaseStepNameByCode(keyStepCode);
    }

    /**
     * 根据当前登录用户已创建的月度产能规划，选中的productionLineCode和planningDate得到关键工序
     *
     * @param listBaseStepByMonthlyPlanningQuery
     * @return
     */
    public List<BaseStepDTO> listBaseStepByMonthlyPlanning(ListBaseStepByMonthlyPlanningQuery listBaseStepByMonthlyPlanningQuery) {
        String productionLineCode = listBaseStepByMonthlyPlanningQuery.getProductionLineCode();
        Long currentUserId = listBaseStepByMonthlyPlanningQuery.getCurrentUserId();
        String planningDate = listBaseStepByMonthlyPlanningQuery.getPlanningDate();
        if (planningDate == null || "".equals(planningDate) || productionLineCode == null || currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Calendar calendar = MethodUtils.getCalendarByDate(planningDate);
        Integer year = calendar.get(Calendar.YEAR);
        Integer month = calendar.get(Calendar.MONTH) + 1;
        return baseStepDao.listBaseStepByMonthlyPlanning(productionLineCode, year, month, currentUserId);
    }


}
